package sopia.conn;

import sopia.cmm.CommonFunct;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class CategoryAdapter {
	 /** Called when the activity is first created. */
	private DBAdapter dbAdapter ;
	private SQLiteDatabase db;  

	public CategoryAdapter(Context context){
		  dbAdapter = new DBAdapter(context);
		  db = dbAdapter.getSqlDB();
	}
	public void beginTransaction(){
		db.beginTransaction();
	}
	public void setTransactionSuccessful(){
		db.setTransactionSuccessful();
	}
	public void endTransaction(){
		db.endTransaction();
	}
	
    public void close() 
    {
    	dbAdapter.close();
    }
    
    public long insertCategory(String name) 
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(SQLQuery.KEY_NAME_CATEGORY, name);
        return db.insert(SQLQuery.DB_CATEG0RY_TABLE, null, initialValues);
    }

    public boolean deleteCategory(String nameCategory) 
    {
          return db.delete(SQLQuery.DB_CATEG0RY_TABLE,CommonFunct.join(SQLQuery.KEY_NAME_CATEGORY,
        										" = '",nameCategory,"'"), null) > 0;
    }

    //---retrieves all the category---
    public Cursor getAllCategory() 
    {
        return db.query(SQLQuery.DB_CATEG0RY_TABLE, new String[] {
        		SQLQuery.KEY_NAME_CATEGORY}, 
                null, 
                null, 
                null, 
                null, 
                null);
    }
/*
    //---retrieves a particular account---
    public Cursor getAccount(String nameAccount) throws SQLException 
    {
        Cursor mCursor =
        	db.query(true, SQLQuery.DB_ACCOUNT_TABLE, new String[] {
                		SQLQuery.KEY_NAME_ACCOUNT, 
                		SQLQuery.KEY_AMOUNT_ACCOUNT}, 
                		SQLQuery.KEY_NAME_ACCOUNT + " = " + nameAccount, 
                		null,
                		null, 
                		null, 
                		null, 
                		null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    //---updates a account---
    public boolean updateAccount( String nameAccount, 
    String amount) 
    {
        ContentValues args = new ContentValues();
        args.put(SQLQuery.KEY_AMOUNT_ACCOUNT, amount);
        return db.update(SQLQuery.DB_ACCOUNT_TABLE, args, 
        		SQLQuery.KEY_NAME_ACCOUNT + " = " + nameAccount, null) > 0;
    }
    */
}
